package de.melistik.smugmugsync;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JToolBar;

import net.miginfocom.swing.MigLayout;
import de.melistik.IconHandler;
import de.melistik.smugmugsync.connect.SmugMugConnection;
import de.melistik.smugmugsync.model.UsersPreferences;

@SuppressWarnings("serial")
public class PreferencesDialog extends JFrame {

	UsersPreferences preferences = new UsersPreferences();
	TextField usernameField = new TextField();
	JPasswordField passwordField = new JPasswordField();

	public PreferencesDialog(String syncStartPath) {
		this.setTitle("jSmugSync Preferences");
		this.setIconImage(IconHandler.vcardEdit.getImage());
		this.setName("jSmugSyncPreferences");
		this.setLayout(new BorderLayout());
		this.setLocationRelativeTo(null);
		this.setSize(500, 200);
		this.setResizable(false);
		this.setLocation(
				(Toolkit.getDefaultToolkit().getScreenSize().width / 2)
						- (this.getWidth() / 2), (Toolkit.getDefaultToolkit()
						.getScreenSize().height / 2) - (this.getHeight() / 2));
		preferences.readSettings();
		preferences.setSyncStartPath(syncStartPath);
		usernameField.setText(preferences.getUsername());
		passwordField.setText(preferences.getPassword());

		JPanel formPanel = new JPanel(new MigLayout("", "[pref!][grow,fill]",
				"[]15[]"));

		addLabelToField(formPanel, "SyncStartPath: ", new JLabel(
				syncStartPath != null ? syncStartPath : "-"));
		addLabelToField(formPanel, "Username: ", usernameField);
		addLabelToField(formPanel, "Password: ", passwordField);

		this.add(formPanel, BorderLayout.CENTER);

		JToolBar bottomBar = new JToolBar();
		bottomBar.setFloatable(false);

		JButton saveButton = new JButton("Save", IconHandler.disk);
		saveButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				SmugMugConnection connection = new SmugMugConnection(
						usernameField.getText(), passwordField.getText());
				if (connection.isLoggedIn()) {
					preferences.setUsername(usernameField.getText());
					preferences.setPassword(passwordField.getText());
					if (!preferences.storeSettings()) {
						JOptionPane.showMessageDialog(PreferencesDialog.this,
								"had problems to store settings : (");
					} else {
						PreferencesDialog.this.setVisible(false);
					}
				} else {
					JOptionPane.showMessageDialog(PreferencesDialog.this,
							"username or password is invalid");
				}
			}
		});
		bottomBar.add(saveButton);
		this.add(bottomBar, BorderLayout.SOUTH);
	}

	static protected void addLabelToField(Container c, String label,
			Component field) {
		JLabel l = new JLabel(label);
		c.add(l);
		l.setLabelFor(field);
		c.add(field, "wrap");
	}
}
